<template>
  <div v-if="error" class="error-boundary">
    <div class="error-content">
      <div class="error-icon">⚠️</div>
      <h2>出错了</h2>
      <p class="error-message">{{ error.message }}</p>
      <div class="error-actions">
        <button class="btn-primary" @click="retry">
          重试
        </button>
        <button class="btn-secondary" @click="dismiss">
          关闭
        </button>
      </div>
      <details v-if="error.details" class="error-details">
        <summary>错误详情</summary>
        <pre>{{ JSON.stringify(error.details, null, 2) }}</pre>
      </details>
    </div>
  </div>
  <slot v-else />
</template>

<script setup lang="ts">
import { ref } from 'vue'
import type { AppError } from '@/types'

const props = defineProps<{
  error?: AppError | null
}>()

const emit = defineEmits<{
  retry: []
  dismiss: []
}>()

const error = ref(props.error)

function retry() {
  error.value = null
  emit('retry')
}

function dismiss() {
  error.value = null
  emit('dismiss')
}
</script>

<style lang="scss" scoped>
.error-boundary {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 300px;
  padding: 2rem;
}

.error-content {
  text-align: center;
  max-width: 500px;
  padding: 2rem;
  background: var(--bg-card);
  border-radius: 12px;
  border: 1px solid rgba(255, 0, 0, 0.3);
  box-shadow: 0 0 20px rgba(255, 0, 0, 0.2);
}

.error-icon {
  font-size: 4rem;
  margin-bottom: 1rem;
  animation: pulse 2s ease-in-out infinite;
}

h2 {
  color: var(--text-primary);
  margin-bottom: 1rem;
  font-size: 1.5rem;
}

.error-message {
  color: var(--text-secondary);
  margin-bottom: 2rem;
  line-height: 1.6;
}

.error-actions {
  display: flex;
  gap: 1rem;
  justify-content: center;
  margin-bottom: 1rem;
}

.btn-primary,
.btn-secondary {
  padding: 0.75rem 1.5rem;
  border-radius: 8px;
  border: none;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 500;
}

.btn-primary {
  background: var(--color-primary);
  color: var(--bg-primary);
  box-shadow: 0 0 10px var(--color-primary);
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 0 20px var(--color-primary);
  }
}

.btn-secondary {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--text-secondary);
  
  &:hover {
    color: var(--text-primary);
    border-color: var(--text-primary);
  }
}

.error-details {
  margin-top: 1rem;
  text-align: left;
  
  summary {
    cursor: pointer;
    color: var(--text-secondary);
    font-size: 0.875rem;
    margin-bottom: 0.5rem;
    
    &:hover {
      color: var(--text-primary);
    }
  }
  
  pre {
    background: var(--bg-primary);
    padding: 1rem;
    border-radius: 8px;
    overflow-x: auto;
    font-size: 0.75rem;
    color: var(--text-secondary);
    max-height: 200px;
  }
}
</style>
